﻿using ShopFine.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace ShopFine.userControls
{
    public partial class UcEditProduct : System.Web.UI.UserControl
    {
        private object _dataItem = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Form.Attributes.Add("enctype", "multipart/form-data");
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        ///          Required method for Designer support - do not modify
        ///          the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.DataBinding += new System.EventHandler(this.CompanyDetails_DataBinding);

        }
        #endregion
        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }

        protected void CompanyDetails_DataBinding(object sender, System.EventArgs e)
        {
            var listType = new SubCategoryDAO().GetAll();
            ddlSubCategory.DataSource = listType;
            ddlSubCategory.DataTextField = "Name";
            ddlSubCategory.DataValueField = "ID";
            ddlSubCategory.DataBind();

            var supplierList = new SupplierDAO().GetAll();
            ddlSupplier.DataSource = supplierList;
            ddlSupplier.DataTextField = "Name";
            ddlSupplier.DataValueField = "ID";
            ddlSupplier.DataBind();

            // Edit data binding here
            if (!(DataItem is GridInsertionObject))
            {
                if (listType.Count > 0)
                {
                    object subCate = DataBinder.Eval(DataItem, "SubCategoryID");
                    if (subCate == null)
                    {
                        subCate = listType.Count > 0 ? listType[0].ID : 0;
                    }

                    var subCateObj = listType.FirstOrDefault(p => p.ID == (int)subCate);
                    ddlSubCategory.SelectedIndex = listType.IndexOf(subCateObj);
                    ddlSubCategory.DataSource = null;
                }

                if (supplierList.Count > 0)
                {
                    object supplierID = DataBinder.Eval(DataItem, "SupplierID");
                    if (supplierID == null)
                    {
                        supplierID = supplierList.Count > 0 ? supplierList[0].ID : 0;
                    }

                    var supplierObj = supplierList.FirstOrDefault(p => p.ID == (int)supplierID);
                    ddlSupplier.SelectedIndex = supplierList.IndexOf(supplierObj);
                    ddlSupplier.DataSource = null;
                }
            }
        }

        public string ProcessMyDataItem(object myValue)
        {
            if (myValue == null)
            {
                return "";
            }

            return myValue.ToString();
        }
    }
}